package com.smile.frame.config.es.utils;

import cn.hutool.core.util.PageUtil;
import cn.hutool.json.JSONUtil;
import com.smile.frame.common.page.PageQuery;
import com.smile.frame.common.page.PageResult;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Es工具
 *
 * @author LGC
 * @date 2021/12/3 10:05
 */
public class EsBaseUtil<T> {


    /**
     * 时间设置
     *
     * @param seconds
     * @return
     */
    public static TimeValue getTimeValue(Integer seconds) {
        return new TimeValue(seconds, TimeUnit.SECONDS);
    }

    /**
     * 分页参数
     *
     * @param searchSourceBuilder
     * @param pageQuery
     */
    public static void setPage(SearchSourceBuilder searchSourceBuilder, PageQuery pageQuery) {
        searchSourceBuilder
                .from(Math.toIntExact((pageQuery.getPage_num() - 1) * pageQuery.getPage_size()))
                .size(Math.toIntExact(pageQuery.getPage_size()));
    }

    /**
     * page结构返回
     *
     * @param response
     * @param clazz
     * @param page
     * @return
     */
    public PageResult<T> buildResultPage(SearchResponse response, Class<T> clazz, PageQuery page) {
        if (response != null) {
            // 获取总条数
            long totalCount = response.getHits().getTotalHits().value;
            // 获取分页数据
            SearchHit[] hits = response.getHits().getHits();
            // 数据封装
            List<T> dataList = Arrays.stream(hits)
                    .map(item -> JSONUtil.toBean(item.getSourceAsString(), clazz))
                    .collect(Collectors.toList());
            // 构造分页对象
            PageResult<T> result = new PageResult<>();
            result.setPage_num(Math.toIntExact(page.getPage_num()));
            result.setPage_size(Math.toIntExact(page.getPage_size()));
            result.setPage_count(PageUtil.totalPage(Math.toIntExact(totalCount), Math.toIntExact(page.getPage_size())));
            result.setTotal((int) totalCount);
            result.setData(dataList);
            return result;
        }
        return new PageResult<>();
    }


//    public static BulkResponseErrorDTO BulkResponse(BulkResponse bulkResponse) {
//
//        List<String> indexIdList = new ArrayList<>();
//        List<String> updateIdList = new ArrayList<>();
//        List<String> deleteIdList = new ArrayList<>();
//
//        // 循环检测批量操作结果
//        for (BulkItemResponse bulkItemResponse : bulkResponse) {
//            DocWriteResponse itemResponse = bulkItemResponse.getResponse();
//            // 根据操作类型检测执行结果
//            if (!itemResponse.status().equals(RestStatus.OK)) {
//                switch (bulkItemResponse.getOpType()) {
//                    case INDEX:
//                    case CREATE:
//                        // 处理创建请求
//                        IndexResponse indexResponse = (IndexResponse) itemResponse;
//                        indexIdList.add(indexResponse.getId());
//                        break;
//                    case UPDATE:
//                        // 处理更新请求
//                        UpdateResponse updateResponse = (UpdateResponse) itemResponse;
//                        updateIdList.add(updateResponse.getId());
//                        break;
//                    case DELETE:
//                        // 处理删除请求
//                        DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
//                        deleteIdList.add(deleteResponse.getId());
//                }
//            }
//        }
//
//        // 返回结果封装
//        BulkResponseErrorDTO bulkResponseErrorDTO = new BulkResponseErrorDTO();
//        bulkResponseErrorDTO.setIndexIdList(indexIdList);
//        bulkResponseErrorDTO.setUpdateIdList(updateIdList);
//        bulkResponseErrorDTO.setDeleteIdList(deleteIdList);
//        return bulkResponseErrorDTO;
//
//    }
}
